/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-12-30 10:01:48
 * @LastEditors: zhl
 * @LastEditTime: 2022-01-26 15:58:35
 * @Description: 文件实现描述
 */
#ifndef __PARSE_TASK_H__
#define __PARSE_TASK_H__

#include "CTask.h"
#include "CConfigManager.h"
#include "CMosquittoManager.h"
#include "modbus_datastruct.h"
#include "CReportManager.h"
#include "global_data_struct.h"


class ModbusParseTask : public CTask{
private:
    /* data */
    TASK_PARAMS *c_task_params;
    std::shared_ptr<CConfigManager> c_cm;
    std::shared_ptr<CMosquittoManager> c_mm;
    std::list<std::shared_ptr<COLLECT_DATA>> c_data;
    std::unordered_map<std::string, RESPONSE_DATA> c_points_value;
    std::unordered_map<std::string, RESPONSE_DATA> c_response_data;

private:
    int parse_register(std::shared_ptr<COLLECT_DATA> data_ptr, std::unordered_map<int64_t, std::list<int64_t>> &reg_points);
    int parse_coil(std::shared_ptr<COLLECT_DATA> data_ptr, std::unordered_map<int64_t, std::list<int64_t>> &reg_points);
    int get_point_value(std::shared_ptr<MODBUS_POINT>, uint16_t *data, std::string &result);
    int save_point_value(std::shared_ptr<MODBUS_POINT> point, std::string value);
    int parse_alert(std::shared_ptr<MODBUS_POINT> point, int64_t dev_id, std::string value);
    int parse_linkage(std::shared_ptr<MODBUS_POINT> point, int64_t dev_id, std::string value);
    int save_alert(ALERT_DATA &alert);
    int is_linkage(std::string op, std::string value1, std::string value2);
    template<typename T>
    std::string get_real_value(std::shared_ptr<MODBUS_POINT> point, T value);
    uint16_t get_short_value_by_order(std::shared_ptr<MODBUS_POINT> point, uint16_t *data);
    uint32_t get_int_value_by_order(std::shared_ptr<MODBUS_POINT> point, uint16_t *data);
    float    get_float_value_by_order(std::shared_ptr<MODBUS_POINT> point, uint16_t *data);
    int parse_multi_linkage(std::shared_ptr<MODBUS_POINT> point, int64_t dev_id, std::string value);
    int update_linkage_action_last_value(std::shared_ptr<MODBUS_POINT> point, int64_t dev_id, std::string value);

public:
    ModbusParseTask(std::shared_ptr<CConfigManager> cm, 
        std::shared_ptr<CMosquittoManager> mm, 
        TASK_PARAMS *task_params, 
        std::list<std::shared_ptr<COLLECT_DATA>> &data, 
        std::unordered_map<std::string, RESPONSE_DATA> &resp_datas);
    ~ModbusParseTask();

    void run();
    int parse(std::shared_ptr<COLLECT_DATA> data_ptr);

};



#endif /* __PARSE_TASK_H__ */
